package ch.zbw.timeline.server.domain;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

import org.apache.log4j.Logger;

import ch.zbw.timeline.server.ServerApp;


public class ServerImpl extends UnicastRemoteObject implements Server {

	private final static Logger LOGGER = Logger.getLogger(ServerImpl.class);

	private Facade facade = new FacadeMock();
	
	public ServerImpl() throws RemoteException {
		init();
	}

	/**
	 * Initializes the server for RMI
	 */
	private void init() {
		try {
			LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
			Naming.rebind(ServerApp.SERVER, this);
		} catch (RemoteException e) {
			LOGGER.error("Fehler bei der Erstellung des Registry-Eintrages: " + e.getMessage());
		} catch (MalformedURLException e) {
			LOGGER.error("Fehler beim Einbinden der URL zu einem Remote-Objekt: " + e.getMessage());
		}
	}

	/**
	 * Test method for clients
	 */
	public String sayHello() throws RemoteException {
		return "Hello World!";
	}

	public List<Role> getAllRoles() throws RemoteException {
		return facade.getAllRoles();
	}

	public User authenticate(String userName, String password, Role role) throws RemoteException {
		return facade.authenticate(userName, password, role);
	}

}
